
const mongoose = require('mongoose');
var express = require('express')
// 注意这里的声明
var tittleType = express()
const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const {tittleTypeService} = require('../../service/impl/tittleTypeService/index');




tittleType.post('/post/tittles/add/type', async (req,res) => {
  let result
  try {
    let news  = req.body.news
    result =  await tittleTypeService.getIns().create(news)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })


  tittleType.post('/post/tittleType/delete', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      result =  await tittleTypeService.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })


/* 修改题目类型 */
tittleType.post('/post/tittleType/update',async (req,res) => {
  let result
  try {
    let  id  = req.body.conditions.id
    let updates = req.body.updates
    result =  await tittleTypeService.getIns().findByIdAndUpdate(id,updates)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
  createResponse(res,200,businessCodes.b1,result)
})

/* 查询所有题目类型 */
tittleType.post('/post/tittleType/queryAll',async (req,res) => {
  let result
  try {
    result =  await tittleTypeService.getIns().find()
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
})


  module.exports= tittleType